import axios from 'axios';
import qs from 'query-string';
import type { HttpResponse } from './interceptor';

export interface PickUpPointId {
    id: number;
}

export interface PickUpPointRecord {
    id: number;
    name: string;
    province: string;
    city: string;
    area: string;
    address: string;
    gps: string;
    phone: string;
    city_id: number;
    status: number;
    status_text: string;
    created_at: string;
    updated_at: string;
}

export interface PickUpPointListRes {
    list: PickUpPointRecord[];
    total: number;
}

export interface PickUpPointModel {
    id?: number;
    name: string;
    province: string;
    city: string;
    area: string;
    address: string;
    gps: string;
    phone: string;
    city_id: number;
    status: number;
}

export interface PickUpPointParams {
    current: number;
    pageSize: number;
    name?: string;
    city_id?: number;
    status?: number;
}

export interface PickUpPointStatusParams {
    id: number;
    status: number;
}

/**
 * 获取配送点列表
 * @param params
 */
export function queryPickUpPointList(params: PickUpPointParams): Promise<HttpResponse<PickUpPointListRes>> {
    return axios.get('/admin/pickUpPoint/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取配送点详情
 * @param params
 */
export function queryPickUpPointOne(params: PickUpPointId): Promise<HttpResponse<PickUpPointRecord>> {
    return axios.get('/admin/pickUpPoint/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 创建配送点
 * @param data
 */
export function createPickUpPoint(data: PickUpPointModel): Promise<HttpResponse<{ id: number }>> {
    return axios.post('/admin/pickUpPoint/create', data);
}

/**
 * 更新配送点
 * @param data
 */
export function updatePickUpPoint(data: PickUpPointModel): Promise<HttpResponse<null>> {
    return axios.post('/admin/pickUpPoint/update', data);
}

/**
 * 删除配送点
 * @param id
 */
export function delPickUpPoint(id: number): Promise<HttpResponse<null>> {
    return axios.post('/admin/pickUpPoint/delete', { id });
}

/**
 * 更新配送点状态
 * @param params
 */
export function updatePickUpPointStatus(params: PickUpPointStatusParams): Promise<HttpResponse<null>> {
    return axios.post('/admin/pickUpPoint/updateStatus', params);
}


export interface PickUpPointDayGoodsParams {
    current: number;
    pageSize: number;
  }
  
  export interface PickUpPointDayGoodsListItem {
    id: number;
    username: string;
    status: number;
    created_at: string;
    updated_at: string;
  }
  
  export interface PickUpPointDayGoodsListRes {
    list: PickUpPointDayGoodsListItem[];
    total: number;
  }
  
  export function getPickUpPointDayGoodsList(params: any) {
    return axios.get('/admin/pickUpPoint/dayGoodsList', { params });
  }

  export function getPickUpPointDayGoodsListByPage(params: PickUpPointDayGoodsParams) {
    return axios.get<PickUpPointDayGoodsListRes>('/admin/pickUpPoint/dayGoodsListByPage', { params });
  }

// 配送点订单数据汇总相关接口
export interface PickupOrderItem {
  goods_id: number;
  goods_title: string;
  sku_spec_names: string;
  quantity: number;
  price: number;
  total_price: number;
}

export interface PickupOrder {
  order_id: number;
  order_no: string;
  user_phone: string;
  pay_time: string;
  pay_amount: number;
  total_amount: number;
  discount_amount: number;
  items: PickupOrderItem[];
}

export interface PickupPointInfo {
  id: number;
  name: string;
  address: string;
  phone: string;
  province: string;
  city: string;
}

export interface PickupOrdersGroup {
  pickup_point: PickupPointInfo | null;
  orders: PickupOrder[];
}

export interface PickupOrdersSummaryRes {
  list: PickupOrdersGroup[];
  total_pickup_points: number;
  total_orders: number;
  query_date: string;
}

export interface PickupOrdersSummaryParams {
  date: string;
}

/**
 * 获取配送点订单数据汇总
 * @param params
 */
export function getPickupOrdersSummary(params: PickupOrdersSummaryParams): Promise<HttpResponse<PickupOrdersSummaryRes>> {
  return axios.get('/admin/pickUpPoint/pickupOrdersSummary', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}